package jsd2108.game.server;

import jsd2108.game.server.entity.Hero;
import jsd2108.game.server.entity.OptInfo;
import jsd2108.game.server.service.OptService;
import jsd2108.game.server.service.SocketService;
import lombok.SneakyThrows;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GameApplication {


    public static void main1(String[] args) {

        List<Hero> heroList=new ArrayList<Hero>();
        heroList.add(new Hero().setHp(100).setName("haha").setX(100).setName("a"));
        heroList.add(new Hero().setHp(100).setName("haha").setX(100).setName("b"));

        OptInfo optInfo=new OptInfo();
        optInfo.setOpt("D").setName("a");

        new OptService(optInfo,heroList).opt();

    }

    //这个注解自动捕获异常
    @SneakyThrows
    public static void main(String[] args) {

        ServerSocket serverSocket = new ServerSocket();
        List<Socket> clientSocket = new ArrayList<Socket>();
        List<Hero> heroList = new ArrayList<Hero>();

        //等待两个客户端连接
        for (int i = 0; i < 2; i++) {
            Socket socket = serverSocket.accept();
            clientSocket.add(socket);

            //接收前端传来的姓名
            //......

            //接收名字
            String heroName = "陈小"+i; //改成客户端传来接收

            Hero hero=new Hero()
                    .setName(heroName)
                    .setX(100) //默认横向坐标
                    .setY(0) //默认纵向坐标
                    .setHp(100); //默认血量

            heroList.add(hero);
            System.out.println(new Date()+"\t英雄["+ heroName+"]进入战场");
        }

        SocketService socketService=new SocketService(serverSocket,clientSocket,heroList);

        while (true){
            socketService.sent();
        }
    }

}
